package com.chenzhou.product_sys.dao.impl;

import com.chenzhou.product_sys.common.BaseDAO;
import com.chenzhou.product_sys.entity.ImagesEntity;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ImagesDAOImpl extends BaseDAO<ImagesEntity>{
    @Override
    protected List<ImagesEntity> resultSetToList(ResultSet rs) throws SQLException {
        List<ImagesEntity> list = new ArrayList<>();
        while(rs.next()){
            ImagesEntity images = new ImagesEntity();
            images.setId(rs.getInt("id"));
            images.setFilename(rs.getString("filename"));
            images.setFile_url(rs.getString("file_url"));
            images.setFile_pix(rs.getString("file_pix"));
            images.setGood_id(rs.getInt("good_id"));
            images.setMarkup(rs.getString("markup"));
            list.add(images);
        }
        return list;
    }

    public List<ImagesEntity> queryImagesByGoodsID(Integer id){
        String sql = "select * from tbl_images where good_id = ?";
        Object[] objects = new Object[]{id};
        return this.execute_Query(sql,objects);
    }

    public int add_goodsImage(ImagesEntity imagesEntity){
        String sql = "insert into tbl_images values(null,?,?,?,?,?)";
        Object[] objects = new Object[]{
                imagesEntity.getFilename(),
                imagesEntity.getFile_url(),
                imagesEntity.getFile_pix(),
                imagesEntity.getGood_id(),
                imagesEntity.getMarkup()
        };
        return this.execute_modify(sql,objects);
    }

    public int delete_goodsImage(Integer id){
        String sql = "delete from tbl_images where id = ?";
        Object[] objects = new Object[]{id};
        return this.execute_modify(sql,objects);
    }
}
